#include "view_time_2.h"
#include "view_time_2.inc"
#include "view_times.h"
#include "display_ctrl.h"
#include "platform_assert.h"
// #include "view_time.inc"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#if defined(TIME_2_SKIP)
#else
void view_time_2_display(void)
{
    disp_draw_user_image(0, 0, time_2_back);
    // int year = 0;
    int month = 0;
    int day = 0;
    int week = 0;
    int hour = 0;
    int minute = 0;
    int steps = 0;
    int distances = 0;
    int show_distances = 0;
    int calories = 0;
    int target_steps = 0;
    int target_distances = 0;
    int target_calories = 0;
    // year = disp_ctrl.com_data.i_temp[8] + 2000;
    month = disp_ctrl.com_data.i_temp[9];
    day = disp_ctrl.com_data.i_temp[10];
    week = disp_ctrl.com_data.i_temp[11] - 1;
    hour = disp_ctrl.com_data.i_temp[5];
    minute = disp_ctrl.com_data.i_temp[6];
    steps = disp_ctrl.com_data.i_temp[2];
    calories = disp_ctrl.com_data.i_temp[3];
    distances = disp_ctrl.com_data.i_temp[18];
    show_distances = disp_ctrl.com_data.i_temp[16];
    target_steps = disp_ctrl.com_data.i_temp[14];
    target_distances = disp_ctrl.com_data.i_temp[17];
    target_calories = disp_ctrl.com_data.i_temp[15];
    // steps = 500;
    // calories = 1000;
    // distances = 2000;
    int x = 0;
    int y = 0;
    y = 63;
    // slash_8x14
    {
        // int num_height = 12;
        int num_width = 8;
        int num_interval = 1;
        int num_total = 4;
        int slash_width = 8;
        int slash_interval = 1;
        int week_interval = 9;
        uint8_t const *const week_bmp = time2_week_TAB[week];
        int week_width = week_bmp[0];
        int total_width = num_total * num_width + 2 * num_interval + slash_width + 2 * slash_interval + week_width + week_interval;
        x = (240 - total_width) / 2;
        // x = disp_draw_user_image_num_with_digits(x, y, year, 4, num_width + num_interval, num_8x12_TAB);
        // disp_draw_rect_color(x, y + num_height / 2, x + num_width - 1, y + num_height / 2 + 1, RGB565_COLOR_WHITE);
        // x += num_width + num_interval;
        x = disp_draw_user_image_num_with_digits(x, y, month, 2, num_width + num_interval, num_8x12_TAB);
        x -= num_interval;
        x += slash_interval;
        disp_draw_user_image(x, y - 1, slash_8x14);
        x += slash_width + slash_interval;
        x = disp_draw_user_image_num_with_digits(x, y, day, 2, num_width + num_interval, num_8x12_TAB);
        x -= num_interval;
        x += week_interval;
        disp_draw_user_image(x, y, week_bmp);
        // disp_draw_rect_color(x, y + num_height / 2, x + num_width - 1, y + num_height / 2 + 1, RGB565_COLOR_WHITE);
        // x += num_width + num_interval;
        // x = disp_draw_user_image_num_with_digits(x, y, day, 2, num_width + num_interval, num_8x12_TAB);
    }
    // {
    //     y = 62;
    //     uint8_t const *const week_bmp = time2_week_TAB[week];
    //     x = (240 - week_bmp[0]) / 2;
    //     disp_draw_user_image(x, y, week_bmp);
    // }
    {
        y = 81;
        x = 30;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_m005_time_0_30_79 + (hour / 10) % 10);
        x = 71;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_m005_time_0_30_79 + (hour) % 10);
        x = 133;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_m005_time_0_30_79 + (minute / 10) % 10);
        x = 175;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_m005_time_0_30_79 + (minute) % 10);
    }
    int num_width = 10;
    int num_height = 14;
    int num_interval = 1;
    {
        x = 67;
        int width = 75;
        y = 145;
        uint16_t color = 0x8e03;
        int show_width = 0;
        if (steps >= target_steps)
        {
            show_width = width;
        }
        else if (target_steps != 0)
        {
            show_width = steps * width / target_steps;
        }
        disp_draw_rect_color(x, y, x + show_width - 1, y + 10 - 1, color);
        x += show_width + 5;
        y = 142;
        if (steps > 0)
        {
            disp_draw_user_image_num(x, y, steps, num_width + num_interval, num_10x14_TAB);
        }
    }
    {
        x = 67;
        int width = 90;
        y = 170;
        uint16_t color = 0x2d5c;
        int show_width = 0;
        if (distances >= target_distances)
        {
            show_width = width;
        }
        else if (target_distances != 0)
        {
            show_width = distances * width / target_distances;
        }
        disp_draw_rect_color(x, y, x + show_width - 1, y + 10 - 1, color);
        x += show_width + 5;
        y = 167;
        int num = show_distances / 1000;
        if (show_distances % 1000 / 10 > 0)
        {
            x = disp_draw_user_image_num(x, y, num, num_width + num_interval, num_10x14_TAB);
            disp_draw_rect_color(x, y + num_height - 2, x + 1, y + num_height - 1, RGB565_COLOR_WHITE);
            x += 3;
            num = show_distances % 1000 / 10;
            x = disp_draw_user_image_num_with_digits(x, y, num, 2, num_width + num_interval, num_10x14_TAB);
        }
    }
    {
        x = 67;
        int width = 58;
        y = 192;
        uint16_t color = 0xe002;
        int show_width = 0;
        if (calories >= target_calories)
        {
            show_width = width;
        }
        else if (target_distances != 0)
        {
            show_width = calories * width / target_calories;
        }
        // int show_width = calories * width / target_calories;
        disp_draw_rect_color(x, y, x + show_width - 1, y + 10 - 1, color);

        x += show_width + 5;
        y = 189;
        if (calories > 0)
        {
            disp_draw_user_image_num(x, y, calories, num_width + num_interval, num_10x14_TAB);
        }
    }
}
#endif

